package com.dna.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dna.entity.RoleEntity;
import com.dna.service.RoleService;
import com.dna.utils.Query;
import com.dna.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/role")
@Api("角色信息")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("roleList")
    @ApiOperation(value = "查询所有角色")
    public R role() {
        List<RoleEntity> roleList = roleService.queryAll();

        return R.ok().put("roleList", roleList);
    }

    @GetMapping("list")
    @ApiOperation(value = "查询角色列表")
    public R list(@RequestParam Map<String, Object> map) {

        Query<RoleEntity> query = new Query<RoleEntity>(map, RoleEntity.class);

        Long total = roleService.queryTotal(query);

        List<RoleEntity> roleList = roleService.queryPageList(query);

        return R.ok(total, roleList);

    }

    @PostMapping
    @ApiOperation(value = "保存角色")
    public R saveRole(@RequestBody RoleEntity ro) {

        RoleEntity RoleEntity = roleService.queryOne(ro);
        if (RoleEntity != null) {
            return R.error(ro.getName() + " 已存在!");
        }
        roleService.save(ro);

        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新用户")
    public R updateRole(@RequestBody RoleEntity ro) {
        roleService.update(ro);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除角色")
    public R deleteRole(@RequestBody Set<Long> ids) {

        roleService.delete(ids);
        return R.ok();
    }
}